package be.kuleuven.peno3.mobiletoledo.view.course;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import be.kuleuven.peno3.mobiletoledo.R;
import be.kuleuven.peno3.mobiletoledo.view.ScheduleView;
import be.kuleuven.peno3.mobiletoledo.view.messages.List;
import be.kuleuven.peno3.mobiletoledo.view.multiplechoice.ManageQuestions;


public class CourseView extends Activity {
	private GridView grid;
	/**
	 * Initialize the activity, add menuitems to list.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		displayCourseMenu(); 
		}
	
	private void displayCourseMenu(){
		setContentView(R.layout.coursemenu);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		TextView main_text = (TextView) findViewById(R.id.main_title);
		main_text.setText(this.getIntent().getExtras().getString("name").trim());
		ImageView calendar=(ImageView) findViewById(R.id.calendar);
		calendar.setImageDrawable(this.getResources().getDrawable(R.drawable.calendar));
		ImageView messages=(ImageView) findViewById(R.id.messages);
		messages.setImageDrawable(this.getResources().getDrawable(R.drawable.messages));
		ImageView announcements=(ImageView) findViewById(R.id.announcements);
		announcements.setImageDrawable(this.getResources().getDrawable(R.drawable.announcements));
		ImageView question=(ImageView) findViewById(R.id.question);
		question.setImageDrawable(this.getResources().getDrawable(R.drawable.question));
//		
	} 
	
	/**
	 * Called when the user clicks on a menu-item.
	 * @param view The view that was clicked
	 */
	public void clickListener(View view) {
		ImageView image=(ImageView) view;
		Intent i = null;
		if(image.getId()==R.id.calendar) {
			onClickAnimation(image);
			i=new Intent(this, ScheduleView.class);
		}
		else if(image.getId()==R.id.messages) {
			onClickAnimation(image);
			i=new Intent(this, List.class);
		}
		else if(image.getId()==R.id.announcements) {
			onClickAnimation(image);
			i=new Intent(this, AnnouncementView.class);
		}
		else if(image.getId()==R.id.question) {
			onClickAnimation(image);
			i=new Intent(this, ManageQuestions.class);
		}
		i.putExtra("course_id",this.getIntent().getExtras().getInt("id"));
		activityStartDelay(i);
	}
	
	private void onClickAnimation(View view){
		view.startAnimation(AnimationUtils.loadAnimation(this, R.layout.image_click));
	}
	
	private void activityStartDelay(Intent intent){
		int delay = 400; //Time to delay activity startup
		final Intent i = intent;
		final Handler handler = new Handler(); 
        Timer t = new Timer(); 
        t.schedule(new TimerTask() { 
        	public void run() { 
        		handler.post(new Runnable() { 
        			public void run() { 
        				startActivity(i); 
        			} 
        		}); 
        	} 
        }, delay);
	}
}
